﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Bank
{
    public partial class ATMwork : Form
    {
        private ATM at = new ATM(Login.logForm.getBank());

        public ATMwork()
        {
            InitializeComponent();
        }

        //非法输入检验
        private bool IsLegal(string a)
        {
            //字符串为空则直接返回false
            if (a == "")
                return false;
            Regex rx = new Regex("^[0-9]*$");
            return rx.IsMatch(a);
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            //非法输入检验
            if (!IsLegal(saveQuan.Text))
            {
                MessageBox.Show("非法输入，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int badCashQuan = at.save(Login.logForm.getLoginAcc(), int.Parse(saveQuan.Text));
            MessageBox.Show(string.Format("存款成功，坏钞共{0}元未存入", badCashQuan));
        }

        private void withDrawBtn_Click(object sender, EventArgs e)
        {
            //非法输入检验
            if (!IsLegal(takeQuan.Text))
            {
                MessageBox.Show("非法输入，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (at.withdraw(Login.logForm.getLoginAcc(), int.Parse(takeQuan.Text)))
                MessageBox.Show("取款成功");
            else
                MessageBox.Show("余额不足，取款失败");
        }

        private void changeBtn_Click(object sender, EventArgs e)
        {
            //非法输入检验
            if (!IsLegal(newPwd.Text))
            {
                MessageBox.Show("非法输入，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            at.changePwd(Login.logForm.getLoginAcc(), int.Parse(newPwd.Text));
            MessageBox.Show("修改密码成功");
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            int rest = at.getRest(Login.logForm.getLoginAcc());
            MessageBox.Show(string.Format("查询成功，账户余额为{0}元", rest));
        }

        private void ATMwork_Load(object sender, EventArgs e)
        {
            this.ControlBox = false;
            at.BigMoneyFetched += warning;
        }

        private void backBtn_Click(object sender, EventArgs e)
        {
            this.Close();
            Login.chooseForm.Show();
        }       

        private void searchTotalBtn_Click(object sender, EventArgs e)
        {
            int rest = at.getTotalCred(Login.logForm.getLoginAcc());
            MessageBox.Show(string.Format("查询成功，信用额度为{0}元", rest));
        }

        private void searchRestBtn_Click(object sender, EventArgs e)
        {
            int rest = at.getRestCred(Login.logForm.getLoginAcc());
            MessageBox.Show(string.Format("查询成功，剩余信用额度为{0}元", rest));
        }

        private void searchDateBtn_Click(object sender, EventArgs e)
        {
            DateTime date = at.getPayDay(Login.logForm.getLoginAcc());
            MessageBox.Show(string.Format("查询成功，还款日期为{0}年{1}月{2}日", date.Year, date.Month, date.Day));
        }

        public void warning(BigMoneyArgs e)
        {
            MessageBox.Show(string.Format("当前账户号为{0}的账户正在取走大笔金额，取款数为{1}", e.getAcc(), e.getQuan()), "大额取款警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
